package com.jianyun.jyzs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.hannesdorfmann.mosby3.mvp.MvpActivity;
import com.jianyun.jyzs.R;
import com.jianyun.jyzs.adapter.NewCustomAdapter;
import com.jianyun.jyzs.adapter.OaCustomAdapter;
import com.jianyun.jyzs.bean.OaAppBean;
import com.jianyun.jyzs.bean.OaCustomBean;
import com.jianyun.jyzs.bean.TopBrodCastBean;
import com.jianyun.jyzs.common.CustomLinearLayoutManager;
import com.jianyun.jyzs.presenter.NewCustomPrestener;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.UtilsStyle;
import com.jianyun.jyzs.view.iview.INewCustomView;
import com.jrmf360.rylib.common.util.ToastUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yangshijun on 2018/10/23.
 */

public class NewCustomActivity extends MvpActivity<INewCustomView, NewCustomPrestener> implements INewCustomView {


    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private String userId;

    private NewCustomAdapter oaCustomAdapter;
    private String enterpriseCode;
    private String help_url;
    private List<OaAppBean> oaBeanList;
    private TopBrodCastBean topBrodCastBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        UtilsStyle.setStatusBarMode(this, true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_custom);
        ButterKnife.bind(this);
        initData();
        initView();
        Log.i("test","跳转：");
    }

    private void initData() {

        userId = LoginCache.getInstance().getUserInfo().getUserId();
        enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();
        OaAppBean.ItemBean itemBean = (OaAppBean.ItemBean) getIntent().getSerializableExtra("itemBean");
        tvTitle.setText(itemBean.getAppName());
        String appInfo = itemBean.getAppInfo();
        String erpRootUrl = LoginCache.getInstance().getLoginCache().getErpRootUrl();
        String url = erpRootUrl + appInfo;
        Log.i("test", "应用扩展：" + url);
        getPresenter().getMainCustom(url);
    }

    private void initView() {
        oaCustomAdapter = new NewCustomAdapter(this, null, this);
        oaCustomAdapter.setMyGridClickListener(new NewCustomAdapter.OnMyGridViewClickListener() {
            @Override
            public void onItemClick(int recyclerPostion, int myGridPositon) {
                OaAppBean.ItemBean itemBean = oaBeanList.get(recyclerPostion).getItems().get(myGridPositon);
                int enabled = itemBean.getEnabled();
                if (1 == enabled) {
                    String appType = itemBean.getAppType();
                    if (appType.equals("app")) {
                        startNative(itemBean.getAppInfo(),itemBean.getAppName());
                    } else if (appType.equals("web")) {
                        startHtml(itemBean.getAppInfo(), itemBean.getAppName());
                    } else if (appType.equals("mainFrm")) {
                        startCustomActivity(itemBean);
                    }
                } else {
                    ToastUtil.showToast(NewCustomActivity.this, "权限未开放！");
                }

            }
        });
        oaCustomAdapter.setOnVpClickListener(new NewCustomAdapter.OnVpClickListener() {
            @Override
            public void onVpClick(int postion) {
                if (topBrodCastBean != null) {
                    String jumpUrl = topBrodCastBean.getList().get(postion).getJumpUrl();
                    Intent intent = new Intent(NewCustomActivity.this, HtmlActivity.class);
                    intent.putExtra("urlUserHelper", jumpUrl);
                    startActivity(intent);
                }
            }
        });


        recyclerView.setLayoutManager(new CustomLinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(oaCustomAdapter);

    }

    private void startCustomActivity(OaAppBean.ItemBean itemBean) {
        Intent intent = new Intent(this, NewCustomActivity.class);
        intent.putExtra("itemBean",itemBean);
        startActivity(intent);
    }

    private void startHtml(String appInfo, String appName) {
        String url;
        if (appInfo.contains("employeeid")){
             url = appInfo.replaceAll("\\{ employeeid \\}", userId);
        }else {
            url=appInfo;
        }
        Intent intent = new Intent(this, HtmlActivity.class);
        intent.putExtra("url", appInfo);
        startActivity(intent);
    }

    private void startNative(String appInfo,String appName) {
        ToastUtil.showToast(this,appName);
    }


    @NonNull
    @Override
    public NewCustomPrestener createPresenter() {
        return new NewCustomPrestener();
    }

    @Override
    public void onLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onFailed() {

    }

    @Override
    public void onSuccess(OaCustomBean oaCustomBean) {

        help_url = oaCustomBean.getHelp_url();

        String topBroadcast = oaCustomBean.getTopBroadcast();
        if (topBroadcast != null && !TextUtils.isEmpty(topBroadcast)) {
            getPresenter().getTopCastBean(topBroadcast);
        }
        oaCustomAdapter.setBannerImg(topBroadcast);

        this.oaBeanList = oaCustomBean.getAppList();
        oaCustomAdapter.setOaBeanList(oaBeanList);
    }

    @Override
    public void onSuccess2(List<OaAppBean> oaBeanList) {
        this.oaBeanList = oaBeanList;
        oaCustomAdapter.setOaBeanList(oaBeanList);
    }

    @Override
    public void onGetTopCast(TopBrodCastBean topBrodCastBean) {
        this.topBrodCastBean = topBrodCastBean;
        if (topBrodCastBean.getResult()) {
            oaCustomAdapter.setTopBrodCast(topBrodCastBean.getList());
        }
    }


    @OnClick(R.id.topBack)
    public void onViewClicked() {
        finish();
    }
}
